package com.ias.agdis_r.volley.callback;

import com.android.volley.Response;
import com.ias.agdis_r.activity.MainActivity;
import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.manager.ServiceManage;
import com.ias.agdis_r.module.ParameterModel;
import com.ias.agdis_r.terminalmanager.restful.base.endpoint.response.BaseResponse;
import com.ias.agdis_r.terminalmanager.restful.base.endpoint.response.BaseResponseHeader;
import com.ias.agdis_r.util.S;

/**
 * 对服务器返回数据回调进行统一封装，根据返回的header判断不同的状态执行不同的回调逻辑
 * 
 * @author Chipor
 *
 * @param <T>
 */
public abstract class BaseOnVolleySuccessListener<T extends BaseResponse> implements Response.Listener<T> {

	public static final String TAG = BaseOnVolleySuccessListener.class.getSimpleName();

	@Override
	public final void onResponse(T resp) {
		if (resp.getHeader().getStatus() == BaseResponseHeader.STATUS_SUCCESS) {
			onRequestSuccess(resp);
		}else if (resp.getHeader().getStatus() == BaseResponseHeader.STATUS_FAILED) {
			
			if(S.isNotEmpty(resp.getHeader().getErrorCode())&&resp.getHeader().getErrorCode().equals(BaseResponseHeader.ERRORCODE_UNLOGIN))
			{
				//注销
				ServiceManage.getInstance().onLogonOut();
				EffectManager.getInstance().vibrate();
				EffectManager.getInstance().playSound(ParameterModel.MUSIC_EFFECT_USER_OFFLINE);
				S.startActivity(MainActivity.class);
//				//跳转到登录页面
//				ServiceManage.getInstance().startLoginActivity();
			}
			else
			{
				onRequestFailed(resp.getHeader());
			}
		}

	}

	/**
	 * 服务器返回状态码为1时回调
	 * 
	 * @param response
	 */
	public abstract void onRequestSuccess(T response);

	/**
	 * 服务器状态码为0时回调
	 * 
	 * @param header
	 */
	public abstract void onRequestFailed(BaseResponseHeader header);

}
